function run() {
    var userDetail = document.getElementById('table')
    console.log(userDetail);
    // const userll = document.createElement('tr')
    var trall = document.createElement('tr');
    trall.innerHTML = `<th>
    ID
</th>
<th>
    用户名
</th>
<th>
    密码
</th>
<th>
    手机号
</th>
<th>
    邮箱
</th>
<th>
    头像地址
</th>
<th>
    注册时间
</th>
<th>
    操作
</th>`;
    userDetail.appendChild(trall);
    // userDetail.appendChild(userll)

}
// kan();


var token = localStorage.getItem('mytoken')
    // console.log(token_);

function kan() {
    $.ajax({
        url: `http://192.168.3.184:7001/users`,
        method: "GET",
        headers: {
            token
            // : localStorage.getItem('mytoken')
        },
        success: function(res) {
            // console.log(res.data)
            if (res.code === 0) {

                var trall = document.createElement('tr');
                var userDetail = document.getElementById('table')
                    // userDetail.style.zIndex = 1002
                    // console.log(userDetail);
                    // const userll = document.createElement('tr')

                trall.innerHTML = `<th>
                ID
            </th>
            <th>
                用户名
            </th>
          
            <th>
                手机号
            </th>
            <th>
                邮箱
            </th>
            <th>
                头像地址
            </th>
            <th>
                注册时间
            </th>
            <th>
                操作
            </th>`;

                // userDetail.appendChild(userll)

                userDetail.appendChild(trall)

                for (var i = 0; i < res.data.length; i++) {
                    // console.log(res.data[i].id);


                    // console.log(userDetail);

                    const tr = document.createElement('tr');

                    for (let data in res.data[i]) {

                        // console.log(data)

                        const td = document.createElement("td");
                        td.innerHTML = res.data[i][data]
                        tr.appendChild(td)

                        if (data == 'operate') {
                            td.innerHTML = `<button class="btn" name="delete">删除</button>
                           <button class="btn up" name="g">修改</button>`
                        }

                    }
                    const lasttd = document.createElement('td');
                    lasttd.innerHTML = ` <img src='.\\imges\\edi.png' class="btn up" name="g" data-id='${res.data[i].id}' />
                    <img src='.\\imges\\de.png' class="btn" name="delete" id='${res.data[i].id}' />
                   
                    <img src='.\\imges\\ussss.png' class="btn " name="gt" data-id='${res.data[i].id}' />`
                        //     lasttd.innerHTML = `<button class="btn" name="delete" id='${res.data[i].id}'>删除</button>
                        // <button class="btn up" name="g" data-id='${res.data[i].id}'>修改</button>`
                    tr.appendChild(lasttd)
                    tr.id = res.data[i].id;
                    userDetail.appendChild(tr);
                }
            }

        }
    })
}

// kan()

//增加
function add(name, psd, phone, email) {
    $.ajax({
        url: `http://192.168.3.184:7001/users`,
        type: 'post',
        data: {
            user_name: name,
            user_pswd: psd,
            phone: phone,
            email: email
        },
        success: (res) => {
            console.log(res);

            window.location.reload()
                // kan();

        }
    })
}

//删除
function rem(id) {
    $.ajax({
        url: `http://192.168.3.184:7001/users/${id}`,
        type: 'delete',
        headers: {
            token
        },
        data: { id },
        success: (res) => {
            console.log(res);
            // kan();
            // console.log(id);
            window.location.reload()
        },
        error: () => {
            // console.log();
            console.log(44444444444);
            console.log(token_);
        }
    })
}

//查询
// function getList(keyname = '') {
//     $.ajax({
//         url: `http://192.168.3.184:7001/users`,
//         method: "GET",
//         // headers: {
//         //     token: localStorage.
//         // },
//         success: function(res) {
//             // console.log(userDetail)
//             var userDetail = document.getElementById('table')
//             userDetail.innerHTML = ''
//                 // run();
//             if (res.code === 0) {
//                 // run()
//                 // const tb = document.createElement('tbody');
//                 kan()

//                 // console.log(res);
//             } else {
//                 alert(res.msg)
//             }


//         }
//     })
// }


//搜索
// getList();


const ntt = document.getElementById('pl');
console.log(ntt);
var token = localStorage.getItem('mytoken')
    //获取详情信息
function getTail(id) {
    $.ajax({
        url: `http://192.168.3.184:7001/users/${id}`,
        type: 'get',
        headers: {
            token
        },
        success: (res) => {
            console.log(res);

            for (let item in res.data) {
                const usn = document.querySelector('#usn');
                const ps = document.querySelector('#ps');
                const phone = document.getElementById('phone');
                const email = document.getElementById('email');
                usn.value = res.data.user_name;
                ps.value = res.data.user_pswd;
                phone.value = res.data.phone
                email.value = res.data.email
                    // head.value = res.data.head_portrait;
                console.log(ntt);
                head.text = res.data.head_portrait;
                console.log(head.text);
                console.log(ntt);
                // head.value = head.text;
                ntt.src = `http://192.168.3.184:7001/${head.text}`
                    // console.log(item.user_name);
            }

        }
    })
}


//修改
function update(id, user_name, user_pswd, phone, email, head_portrait, new_pswd) {
    $.ajax({
        url: `http://192.168.3.184:7001/users/${id}`,
        type: 'put',
        data: {
            id,
            user_name,
            user_pswd,
            phone,
            email,
            head_portrait,
            new_pswd

        },
        success: (res) => {
            window.location.reload();
            console.log(res);
        }
    })

    //获取用户信息
    window.onload = function() {
        console.log(detailid);
    }



}